package com.ihighest.app.infox.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.ViewPagerAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 广告页
 * 
 * @author lkx Created at 2015-10-9 下午5:30:58
 * @version 1.0
 */
public class AdvertisementActivity extends BaseActivity implements OnClickListener {
	Intent intent;
	private ViewPager pager;
	private TimeCount timer;
	private TextView countDownTimer;
	private Button jumpOver;
	private List<View> views;
	private boolean empty;
	// 底部小店图片
	private ImageView[] dots;
	private ViewPagerAdapter vpAdapter;
	private ArrayList<String> arrayList;// 图片数据
	private ArrayList<String> actionValue;// 图片行为数据

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_advertisement);
		pager = (ViewPager) findViewById(R.id.viewpager);
		countDownTimer = (TextView) findViewById(R.id.downTime);
		jumpOver = (Button) findViewById(R.id.infox_splashJumpOver);
		empty = getIntent().getBooleanExtra("empty", true);
		if (!empty) {
			arrayList = getIntent().getStringArrayListExtra("imgs");
			actionValue = getIntent().getStringArrayListExtra("actionValue");
		}
		initPager();
		long countTime = 4 * 1000;
		if (empty) {
			countTime = 2 * 1000;
			countDownTimer.setVisibility(View.GONE);
			jumpOver.setVisibility(View.GONE);
		} else {
			jumpOver.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					intent = new Intent(AdvertisementActivity.this, MainActivity.class);

					startActivity(intent);
					finish();
					timer.cancel();
				}
			});
		}
		timer = new TimeCount(countTime, 1000);
		timer.start();
	}

	private void initPager() {
		views = new ArrayList<View>();
		LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		// 初始化广告图片列表
		if (empty) {
			ImageView iv = new ImageView(this);
			iv.setScaleType(ScaleType.CENTER_CROP);
			iv.setLayoutParams(mParams);
			iv.setImageResource(R.drawable.infox_start_bg);
			views.add(iv);
		} else {
			// DisplayImageOptions defaultOptions = new
			// DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.infox_img_loadrong)
			// .showImageOnFail(R.drawable.infox_img_loadrong).showImageOnLoading(R.drawable.infox_img_loadrong).cacheInMemory(true)
			// .cacheOnDisc(true).build();
			for (int i = 0; i < arrayList.size(); i++) {
				ImageView iv = new ImageView(this);
				iv.setScaleType(ScaleType.CENTER_CROP);
				iv.setLayoutParams(mParams);
				iv.setTag(i);
				// ImageLoader.getInstance().displayImage(arrayList.get(i), iv,
				// defaultOptions);
				AppKit.SetNormalImg(arrayList.get(i), iv);
				iv.setOnClickListener(this);
				views.add(iv);
			}
		}

		vpAdapter = new ViewPagerAdapter(views);
		pager.setAdapter(vpAdapter);
		if (!empty) {
			// 初始化底部小点
			initDots();
			pager.setOnPageChangeListener(new ViewPagerPageChangeListener());
		}

	}

	private void initDots() {
		LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		dots = new ImageView[views.size()];
		LinearLayout layout = (LinearLayout) findViewById(R.id.pointGroup);
		if (views.size() == 1) {
			layout.setVisibility(View.INVISIBLE);
		} else {
			for (int i = 0; i < views.size(); i++) {
				dots[i] = new ImageView(AdvertisementActivity.this);
				if (0 == i) {
					dots[i].setBackgroundResource(R.drawable.infox_startadpage_org);
				} else {
					dots[i].setBackgroundResource(R.drawable.infox_startadpage_gray);
				}

				mParams.leftMargin = 5;
				mParams.rightMargin = 5;
				layout.addView(dots[i], mParams);
			}
		}

	}

	/* 定义一个倒计时的内部类 */
	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		@Override
		public void onFinish() {// 计时完毕时触发

			intent = new Intent(AdvertisementActivity.this, MainActivity.class);
			// intent=new
			// Intent(AdvertisementActivity.this,LoginActivity.class);

			startActivity(intent);
			finish();

		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程显示
			countDownTimer.setText((millisUntilFinished / 1000) + "秒");

		}
	}

	class ViewPagerPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int state) {
		}

		@Override
		public void onPageScrolled(int page, float positionOffset, int positionOffsetPixels) {
		}

		@Override
		public void onPageSelected(int page) {
			// 更新图标
			if (views.size() == 1) {

			} else {
				for (int i = 0; i < views.size(); i++) {
					if (page == i) {
						dots[i].setBackgroundResource(R.drawable.infox_startadpage_org);
					} else {
						dots[i].setBackgroundResource(R.drawable.infox_startadpage_gray);
					}
				}
			}

		}

	}

	@Override
	public void onClick(View arg0) {
		int tag = (Integer) arg0.getTag();
		Toast.makeText(AdvertisementActivity.this, "点击了第" + tag + "图:", Toast.LENGTH_SHORT).show();
		Intent intent = new Intent(AdvertisementActivity.this, AdvertisementContentActivity.class);
		intent.putExtra("vlaue", actionValue.get(tag));
		startActivity(intent);
		finish();
		timer.cancel();
	}
}